package com.android.lovely.data

import com.android.lovely.manager.KVStore
import com.android.lovely.user.UserManager.isVip
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

/**
 *
 * @author why
 * @date 2025/3/25 15:26
 */

@JsonClass(generateAdapter = true)
data class SignResponse(
    @Json(name = "sign_days") val checkedDay: Int = 0,// 签到了几天
    @Json(name = "is_sign") val checked: Boolean = false, // 今天是否签到
    @Json(name = "task_list") val dailys: List<SignBean> = listOf(),
    @Json(name = "vip_task_list") val vipDailys: List<SignBean> = listOf()
) {
    fun getRealList(): List<SignBean> {
        return if (isVip()) {
            vipDailys
        } else {
            dailys
        }
    }
}

@JsonClass(generateAdapter = true)
data class SignBean(
    @Json(name = "days") val day: Int = 0,
    @Json(name = "reward") val reward: Int = 0,
    @Json(name = "status") val status: Int = 1,
    @Json(name = "is_sign") val checked: Boolean = false
) {
    fun isToday(daily: SignResponse?): Boolean {
        daily ?: return false
        //只能连续签到，所以根据签到天数来判断，如果今天已签到，checkedDay = today
        val dayOfWeek = if (daily.checked) {
            daily.checkedDay
        } else {
            daily.checkedDay + 1
        }
        return dayOfWeek == day
    }
}

fun isToday(day: String): Boolean {
    return day == SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date())
}

var remoteSigned = false
fun hasSignedToday(): Boolean = remoteSigned || isToday(KVStore.signToday)

fun saveSignShowToday() {
    KVStore.signShowToday = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date())
}

fun saveSignToday() {
    KVStore.signToday = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date())
}

@JsonClass(generateAdapter = true)
data class SignPoint(
    @Json(name = "point") val point: Int = 0,
    @Json(name = "surplus") val balance: Int = 0
)